package com.ivy.drawpaint;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.view.MotionEvent;
import android.view.View;


/**
 * Created by Lenovo on 2017/11/21.
 */

public class MyView extends View {

    private Path mPath;
    private Paint mPaint;

    public MyView(Context context) {
        super(context);

        mPath = new Path();
        mPaint = new Paint();
        mPaint.setColor(Color.RED);
        mPaint.setStrokeWidth(20);
        mPaint.setStyle(Paint.Style.STROKE);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        canvas.drawPath(mPath , mPaint);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        float x = event.getX();
        float y = event.getY();

        switch (event.getAction()){
            case MotionEvent.ACTION_DOWN:
                mPath.moveTo(x , y );
                break;
            case MotionEvent.ACTION_MOVE:
                mPath.lineTo(x , y);
                break;
        }
        invalidate();
        return true;
    }


}
